package com.qg.wcsopcuaqj.dispatch.service.impl;

import com.qg.wcsopcuaqj.dispatch.constant.QGSConstant;
import com.qg.wcsopcuaqj.dispatch.entity.RGV1Entity;
import com.qg.wcsopcuaqj.dispatch.entity.RGV2Entity;
import com.qg.wcsopcuaqj.dispatch.service.DDJService;
import com.qg.wcsopcuaqj.opcua.selfunion.UAService;
import com.qg.wcsopcuaqj.taskPool.QGSThreadPool;
import com.qg.wcsopcuaqj.taskPool.RGV1Runnable;
import com.qg.wcsopcuaqj.taskPool.RGV2Runnable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/16 16:01
 * @Version 1.0
 */
@Service
public class DDJServiceImpl implements DDJService {

    //如果要 用@Autowired 把 单独一个 类 传入一个new出来线程，是不可行的的，
    //因为@Autowired是多处共用一个，传入单独线程，可能会存在线程安全问题。所以不可以
    @Autowired
    private UAService uaService;



    @Override
    public Integer rgv1Task(RGV1Entity rgv1Entity) {
        synchronized (QGSConstant.LOCK){
            RGV1Runnable<Integer> runnable = new RGV1Runnable<>(uaService,rgv1Entity);

            Future<?> futureTask;

            futureTask = QGSThreadPool.rgv1SinglePool.submit(runnable);

            Object taskResult = new Object();
            try {
                taskResult = futureTask.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            Integer taskResults = (Integer) taskResult;
            return taskResults;
        }
    }

    @Override
    public Integer rgv2Task(RGV2Entity rgv2Entity) {
        synchronized (QGSConstant.LOCK){
            RGV2Runnable<Integer> runnable = new RGV2Runnable<>(uaService,rgv2Entity);

            Future<?> futureTask;

            futureTask = QGSThreadPool.rgv2SinglePool.submit(runnable);

            Object taskResult = new Object();
            try {
                taskResult = futureTask.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
            Integer taskResults = (Integer) taskResult;
            return taskResults;
        }
    }


}
